package com.whmskj.xjlhsc.mobie.ProductionManagement.service.imp;

import com.whmskj.xjlhsc.mobie.ProductionManagement.mapper.EnergyConsumptionOfFineSandProductionMapper;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.entity.*;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.vo.*;
import com.whmskj.xjlhsc.mobie.ProductionManagement.service.IEnergyConsumptionOfFineSandProductionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author jiaruchun
 * @date 2025-08-27 09:51
 */
@Slf4j
@Service
public class EnergyConsumptionOfFineSandProductionServiceImpl implements IEnergyConsumptionOfFineSandProductionService {
    @Autowired
    private EnergyConsumptionOfFineSandProductionMapper energyConsumptionOfFineSandProductionMapper;

    @Override
    public EnergyConsumptionOfFineSandProductionListByDayVO getEnergyConsumptionOfFineSandProductionListByDay(LocalDate startDate) {
        EnergyConsumptionOfFineSandProductionListByDayVO energyConsumptionOfFineSandProductionListByDayVO = new EnergyConsumptionOfFineSandProductionListByDayVO();
        if (startDate == null) {
            throw new IllegalArgumentException("startDate cannot be null");
        }
        LocalDate endDate = startDate.minusDays(29); // 向前30天
        List<EnergyConsumptionOfFineSandProductionVO> energyConsumptionOfFineSandProduction = energyConsumptionOfFineSandProductionMapper.getEnergyConsumptionOfFineSandProduction(startDate.toString(), endDate.toString());
        log.info("energyConsumptionOfFineSandProduction:{}", energyConsumptionOfFineSandProduction);
        //当天产量
        Float production = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getProduction();
        energyConsumptionOfFineSandProductionListByDayVO.setProductionAmount(production);
        //当天折合产量
        Float convertIntoProduction = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getConvertIntoProduction();
        energyConsumptionOfFineSandProductionListByDayVO.setProductionAmountConverted(convertIntoProduction);
        //产量列表
        ArrayList<ProductionList> productionLists = new ArrayList<>();
        for (EnergyConsumptionOfFineSandProductionVO energyConsumptionOfFineSandProductionVO : energyConsumptionOfFineSandProduction) {
            productionLists.add(new ProductionList(energyConsumptionOfFineSandProductionVO.getDate(), energyConsumptionOfFineSandProductionVO.getConvertIntoProduction(), energyConsumptionOfFineSandProductionVO.getProduction(), energyConsumptionOfFineSandProductionVO.getMorningShiftProduction(), energyConsumptionOfFineSandProductionVO.getNightShiftProduction()));
        }
        energyConsumptionOfFineSandProductionListByDayVO.setProductionVOList(productionLists);
        //产量列表2
        ArrayList<ProductionList2> productionLists2 = new ArrayList<>();
        for (EnergyConsumptionOfFineSandProductionVO energyConsumptionOfFineSandProductionVO : energyConsumptionOfFineSandProduction) {
            productionLists2.add(new ProductionList2(energyConsumptionOfFineSandProductionVO.getDate(), energyConsumptionOfFineSandProductionVO.getProduction(), energyConsumptionOfFineSandProductionVO.getConvertIntoProduction(), energyConsumptionOfFineSandProductionVO.getEnergyConsumptionPerTonneOfYarn()));
        }
        energyConsumptionOfFineSandProductionListByDayVO.setProductionVOList2(productionLists2);
        //当天能耗
        Float energyConsumption = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getEnergy();
        energyConsumptionOfFineSandProductionListByDayVO.setEnergyConsumption(energyConsumption);
        //当天吨纱耗电
        Float energyConsumptionPerTonneOfYarn = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getEnergyConsumptionPerTonneOfYarn();
        energyConsumptionOfFineSandProductionListByDayVO.setEnergyConsumptionPerTonneOfYarn(energyConsumptionPerTonneOfYarn);
        //能耗列表
        ArrayList<EnergyConsumptionList> energyConsumptionLists = new ArrayList<>();
        for (EnergyConsumptionOfFineSandProductionVO energyConsumptionOfFineSandProductionVO : energyConsumptionOfFineSandProduction) {
            energyConsumptionLists.add(new EnergyConsumptionList(energyConsumptionOfFineSandProductionVO.getDate(), energyConsumptionOfFineSandProductionVO.getEnergy(), energyConsumptionOfFineSandProductionVO.getMorningShiftEnergyConsumption(), energyConsumptionOfFineSandProductionVO.getNightShiftEnergyConsumption(), energyConsumptionOfFineSandProductionVO.getEnergyConsumptionPerTonneOfYarn()));
        }
        energyConsumptionOfFineSandProductionListByDayVO.setEnergyConsumptionList(energyConsumptionLists);
        //效率
        Float efficiency = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getEfficiency();
        //efficiency保留两位小数
        efficiency = Float.parseFloat(String.format("%.2f", efficiency));
        energyConsumptionOfFineSandProductionListByDayVO.setEfficiency(efficiency);
        //开台率
        Float openingTableRate = energyConsumptionOfFineSandProduction.get(energyConsumptionOfFineSandProduction.size() - 1).getOpeningTableRate();
        openingTableRate = Float.parseFloat(String.format("%.2f", openingTableRate));
        energyConsumptionOfFineSandProductionListByDayVO.setOpeningTableRate(openingTableRate);
        //效率列表
        ArrayList<EfficiencyVOList> efficiencyLists = new ArrayList<>();
        for (EnergyConsumptionOfFineSandProductionVO energyConsumptionOfFineSandProductionVO : energyConsumptionOfFineSandProduction) {
            efficiencyLists.add(new EfficiencyVOList(energyConsumptionOfFineSandProductionVO.getDate(),Float.parseFloat(String.format("%.2f", energyConsumptionOfFineSandProductionVO.getEfficiency())) ,Float.parseFloat(String.format("%.2f", energyConsumptionOfFineSandProductionVO.getOpeningTableRate()))));
        }
        energyConsumptionOfFineSandProductionListByDayVO.setEfficiencyList(efficiencyLists);
        ///////////
        for (ProductionList productionList : energyConsumptionOfFineSandProductionListByDayVO.getProductionVOList()) {
            productionList.setProduction(productionList.getProduction() / 1000);
            productionList.setConvertIntoProduction(productionList.getConvertIntoProduction() / 1000);
            productionList.setMorningShiftProduction(productionList.getMorningShiftProduction() / 1000);
            productionList.setNightShiftProduction(productionList.getNightShiftProduction() / 1000);
            //保留两位小数
            productionList.setProduction(Float.parseFloat(String.format("%.2f", productionList.getProduction())));
            productionList.setConvertIntoProduction(Float.parseFloat(String.format("%.2f", productionList.getConvertIntoProduction())));
            productionList.setMorningShiftProduction(Float.parseFloat(String.format("%.2f", productionList.getMorningShiftProduction())));
            productionList.setNightShiftProduction(Float.parseFloat(String.format("%.2f", productionList.getNightShiftProduction())));
        }
        for (ProductionList2 productionList2 : energyConsumptionOfFineSandProductionListByDayVO.getProductionVOList2()) {
            productionList2.setProduction(productionList2.getProduction() / 1000);
            productionList2.setConvertIntoProduction(productionList2.getConvertIntoProduction() / 1000);
            //保留两位小数
            productionList2.setProduction(Float.parseFloat(String.format("%.2f", productionList2.getProduction())));
            productionList2.setConvertIntoProduction(Float.parseFloat(String.format("%.2f", productionList2.getConvertIntoProduction())));
        }
        return energyConsumptionOfFineSandProductionListByDayVO;
    }

    @Override
    public ShiftProductionVO getEnergyConsumptionOfFineSandProductionListByShift(LocalDate date, String dayShiftOrEveningShift) {
        ShiftProductionVO shiftProductionVO = new ShiftProductionVO();
        List<ShiftProductionEnergyVO> shiftProductionEnergy = energyConsumptionOfFineSandProductionMapper.getShiftProductionEnergy(date, dayShiftOrEveningShift + "班");
        log.info("shiftProductionEnergy:{}", shiftProductionEnergy);
        //设置列表
        //shiftProductionEnergy根据stationNumber前两个字符转为的数字大小排序，小在前
        shiftProductionEnergy.sort(Comparator.comparingInt(o -> Integer.parseInt(o.getStationNumber().split("#")[0])));
        shiftProductionVO.setShiftProductionEnergyVOList(shiftProductionEnergy);
        //总机台数
        shiftProductionVO.setOpeningTableNumber(shiftProductionEnergy.size());
        //总产量
        Float totalProduction = 0.0f;
        //总折合产量
        Float totalConvertIntoProduction = 0.0f;
        //平均效率
        Float totalEfficiency = 0.0f;
        for (ShiftProductionEnergyVO shiftProductionEnergyVO : shiftProductionEnergy) {
            totalProduction += shiftProductionEnergyVO.getProduction();
            totalConvertIntoProduction += shiftProductionEnergyVO.getConvertIntoProduction();
            totalEfficiency += shiftProductionEnergyVO.getEfficiency();
        }
        //平均效率
        if (!shiftProductionEnergy.isEmpty()) {
            totalEfficiency = totalEfficiency / shiftProductionEnergy.size();
        }else {
            totalEfficiency = 0.0f;
        }
        //设置总产量
        shiftProductionVO.setProductionAmount(totalProduction);
        //设置总折合产量
        shiftProductionVO.setEquivalentProductionAmount(totalConvertIntoProduction);
        //设置平均效率
        shiftProductionVO.setAverageEfficiency(totalEfficiency);
        //所有小数类型的数据保留三位小数
        shiftProductionVO.setAverageEfficiency(Float.parseFloat(String.format("%.2f", shiftProductionVO.getAverageEfficiency())));
        shiftProductionVO.setProductionAmount(Float.parseFloat(String.format("%.2f", shiftProductionVO.getProductionAmount())));
        shiftProductionVO.setEquivalentProductionAmount(Float.parseFloat(String.format("%.2f", shiftProductionVO.getEquivalentProductionAmount())));
        shiftProductionVO.getShiftProductionEnergyVOList().forEach(shiftProductionEnergyVO -> {
            shiftProductionEnergyVO.setEfficiency(Float.parseFloat(String.format("%.2f", shiftProductionEnergyVO.getEfficiency())));
            shiftProductionEnergyVO.setProduction(Float.parseFloat(String.format("%.2f", shiftProductionEnergyVO.getProduction())));
            shiftProductionEnergyVO.setConvertIntoProduction(Float.parseFloat(String.format("%.2f", shiftProductionEnergyVO.getConvertIntoProduction())));
        });
        return shiftProductionVO;
    }

    @Override
    /*TODO 甲乙丙班数据待完善测试*/
    public MonthlyFineSandStatsVO getEnergyConsumptionOfFineSandProductionListByMonth(String monthStr) {
        String month = monthStr.split("（")[0];
        String monthStart = monthStr.substring(8, 18);
        String monthEnd = monthStr.substring(20, 31);
        MonthlyFineSandStatsVO monthlyFineSandStatsVO = new MonthlyFineSandStatsVO();
        //-----------------------------------------------------------------------班组产量
        ShiftDate shiftDate = new ShiftDate();
        ClassProductionDe classProduction = energyConsumptionOfFineSandProductionMapper.getClassProduction(month);
        //classProduction小数全部取三位
        classProduction.setTotal_production(Float.parseFloat(String.format("%.3f", classProduction.getTotal_production())));
        classProduction.setTotal_efficiency(Float.parseFloat(String.format("%.3f", classProduction.getTotal_efficiency())));
        classProduction.setTotal_energy(Float.parseFloat(String.format("%.3f", classProduction.getTotal_energy())));
        classProduction.setClass_a_production(Float.parseFloat(String.format("%.3f", classProduction.getClass_a_production())));
        classProduction.setClass_a_efficiency(Float.parseFloat(String.format("%.3f", classProduction.getClass_a_efficiency())));
        classProduction.setClass_a_energy(Float.parseFloat(String.format("%.3f", classProduction.getClass_a_energy())));
        classProduction.setClass_y_production(Float.parseFloat(String.format("%.3f", classProduction.getClass_y_production())));
        classProduction.setClass_y_efficiency(Float.parseFloat(String.format("%.3f", classProduction.getClass_y_efficiency())));
        classProduction.setClass_b_production(Float.parseFloat(String.format("%.3f", classProduction.getClass_b_production())));
        classProduction.setClass_b_efficiency(Float.parseFloat(String.format("%.3f", classProduction.getClass_b_efficiency())));
        classProduction.setClass_b_energy(Float.parseFloat(String.format("%.3f", classProduction.getClass_b_energy())));
        //----------------------------------------------------------------------
        ArrayList<ClassProductionList> classProductionLists = new ArrayList<>();
        classProductionLists.add(new ClassProductionList("甲班",classProduction.getClass_a_production(),classProduction.getClass_a_efficiency(),classProduction.getClass_a_energy()));
        classProductionLists.add(new ClassProductionList("乙班",classProduction.getClass_y_production(),classProduction.getClass_y_efficiency(),classProduction.getClass_y_energy()));
        classProductionLists.add(new ClassProductionList("丙班",classProduction.getClass_b_production(),classProduction.getClass_b_efficiency(),classProduction.getClass_b_energy()));
        classProductionLists.add(new ClassProductionList("合计",classProduction.getTotal_production(),classProduction.getTotal_efficiency(),classProduction.getTotal_energy()));
        shiftDate.setClassProductionShiftList(classProductionLists);
        //列表--------------------------------------------------------------------------
        List<ClassProduction> classProductionList = energyConsumptionOfFineSandProductionMapper.getClassProductionList(month);
        //classProductionList所有Float小数保留三位
        for (ClassProduction production : classProductionList) {
            production.setTotalProduction(Float.parseFloat(String.format("%.3f", production.getTotalProduction())));
            production.setA_production(Float.parseFloat(String.format("%.3f", production.getA_production())));
            production.setB_production(Float.parseFloat(String.format("%.3f", production.getB_production())));
            production.setY_production(Float.parseFloat(String.format("%.3f", production.getY_production())));
        }
        shiftDate.setClassProductionList(classProductionList);
        monthlyFineSandStatsVO.setShiftDate(shiftDate);
        //----------------------------------------------------------------------产量
        ClassProductionVO classProductionVO = new ClassProductionVO();
        //当月早晚班产量
        ProdctionVO prodctionVO = energyConsumptionOfFineSandProductionMapper.getProdctionVO(month);
        //三位小数
        prodctionVO.setLateProduction(Float.parseFloat(String.format("%.3f", prodctionVO.getLateProduction())));
        prodctionVO.setEarlyProduction(Float.parseFloat(String.format("%.3f", prodctionVO.getEarlyProduction())));
        classProductionVO.setProdctionVO(prodctionVO);
        //当月早晚班合计产量
        Float totalProduction = prodctionVO.getLateProduction() + prodctionVO.getEarlyProduction();
        classProductionVO.setTotalProduction(totalProduction);
        /*TODO 后面记得改早晚班*/
        List<ClassProductionVOListVO> classProductionVOListVO = energyConsumptionOfFineSandProductionMapper.getClassProductionVOListVO(month);
        classProductionVO.setClassProductionVOList(classProductionVOListVO);
        //当月效率
        EfficiencyVOO efficiencyVOO = energyConsumptionOfFineSandProductionMapper.getEfficiencyVOO(month);
        //三位小数
        efficiencyVOO.setLateEfficiency(Float.parseFloat(String.format("%.3f", efficiencyVOO.getLateEfficiency())));
        efficiencyVOO.setEarlyEfficiency(Float.parseFloat(String.format("%.3f", efficiencyVOO.getEarlyEfficiency())));
        classProductionVO.setEfficiencyVOO(efficiencyVOO);
        monthlyFineSandStatsVO.setClassProductionVO(classProductionVO);
        //---------------------------------------------------------------------月平均效率列表
        EfficiencyListVO efficiencyListVO = new EfficiencyListVO();
        List<EfficiencyListVOList> efficiencyListVOList = energyConsumptionOfFineSandProductionMapper.getEfficiencyListVOList(month);
        for (EfficiencyListVOList listVOList : efficiencyListVOList) {
            listVOList.setEfficiency(Float.parseFloat(String.format("%.2f", listVOList.getEfficiency())));
            listVOList.setEfficiencyJia(Float.parseFloat(String.format("%.2f", listVOList.getEfficiencyJia())));
            listVOList.setEfficiencyYi(Float.parseFloat(String.format("%.2f", listVOList.getEfficiencyYi())));
            listVOList.setEfficiencyBing(Float.parseFloat(String.format("%.2f", listVOList.getEfficiencyBing())));
        }
        efficiencyListVOList.forEach(efficiencyListVOList1 -> {
            if(efficiencyListVOList1.getDate().equals(month)){
                efficiencyListVO.setEfficiencyNowMonth(efficiencyListVOList1.getEfficiency());
            }
        });
        efficiencyListVO.setEfficiencyListVOList(efficiencyListVOList);
        monthlyFineSandStatsVO.setEfficiencyVO(efficiencyListVO);
        return monthlyFineSandStatsVO;
    }

    @Override
    public EnergyConsumptionOfFineSandProductionListByVarietyVO getEnergyConsumptionOfFineSandProductionListByVariety(String monthStr) {
        String monthStart = monthStr.substring(8, 18);
        String monthEnd = monthStr.substring(20, 31);
        EnergyConsumptionOfFineSandProductionListByVarietyVO energyConsumptionOfFineSandProductionListByVarietyVO = new EnergyConsumptionOfFineSandProductionListByVarietyVO();
        List<MonthlyVarietyDetailVO> monthlyVarietyDetail = energyConsumptionOfFineSandProductionMapper.getMonthlyVarietyStats(monthStart,monthEnd);
        for (MonthlyVarietyDetailVO monthlyVarietyDetailVO : monthlyVarietyDetail) {
            //小数保留两位
            monthlyVarietyDetailVO.setTotalProduction(Float.parseFloat(String.format("%.2f", monthlyVarietyDetailVO.getTotalProduction())));
            monthlyVarietyDetailVO.setTotalConvertIntoProduction(Float.parseFloat(String.format("%.2f", monthlyVarietyDetailVO.getTotalConvertIntoProduction())));
            monthlyVarietyDetailVO.setTotalEnergy(Float.parseFloat(String.format("%.2f", monthlyVarietyDetailVO.getTotalEnergy())));
            monthlyVarietyDetailVO.setEnergyPerTonne(Float.parseFloat(String.format("%.2f", monthlyVarietyDetailVO.getEnergyPerTonne())));
        }
        //总产量
        Float totalProduction = 0.0f;
        //总折合产量
        Float totalConvertIntoProduction = 0.0f;
        for (MonthlyVarietyDetailVO monthlyVarietyDetailVO : monthlyVarietyDetail) {
            totalProduction += monthlyVarietyDetailVO.getTotalProduction();
            totalConvertIntoProduction += monthlyVarietyDetailVO.getTotalConvertIntoProduction();
        }
        log.info("monthlyVarietyDetail:{}", monthlyVarietyDetail);
        energyConsumptionOfFineSandProductionListByVarietyVO.setTotalProduction(totalProduction);
        energyConsumptionOfFineSandProductionListByVarietyVO.setTotalConvertIntoProduction(totalConvertIntoProduction);
        energyConsumptionOfFineSandProductionListByVarietyVO.setMonthlyVarietyDetailVOList(monthlyVarietyDetail);
        return energyConsumptionOfFineSandProductionListByVarietyVO;
    }
}
